package com.guli.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.guli.serviceBase.Entity.GuliExcepiton;
import com.guli.vod.service.VodService;
import com.guli.vod.utils.AliyuVodClientUtil;
import com.guli.vod.utils.ConstantPropertiesGet;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Random;

/**
 * @author zxc
 * @date 2023/04/08 17:12
 **/
@Service
public class VodServiceImpl implements VodService {
    /**
     * 具体实现 上传音视频文件到阿里云Vod中;
     * @param file
     * @return
     */
    @Override
    public String uploadVideo(MultipartFile file) {
        // 1.获取application.yml文件中，所配置的keyId和keySecret;
        String keyId = ConstantPropertiesGet.KEY_ID;
        String keySecret = ConstantPropertiesGet.KEY_SECRET;

        try {
            // 2.获取所要上传文件的输入流InputStream;
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            Random random = new Random();
            String title = "zxc" + random.nextInt(1000) + " - "+ fileName;
            // 3.流式上传文件;
            // 即，创建流式上传音视频文件所对应的request;
            UploadStreamRequest request = new UploadStreamRequest(keyId , keySecret , title , fileName , inputStream);
            // 4.创建上传音视频文件的具体实现类
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            // 未能够上传音视频成功的情况 ：
            if(!response.isSuccess()){
                throw new GuliExcepiton(20001 , "上传音视频文件失败!!!");
            }
            return response.getVideoId();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

    }

    // 具体实现 根据音视频id，去删除阿里云Vod端中指定的音视频文件;
    @Override
    public void deleteVideoVod(String videoSourceId) {
        // 1.获取application.yml文件中，所配置的keyId和keySecret;
        String keyId = ConstantPropertiesGet.KEY_ID;
        String keySecret = ConstantPropertiesGet.KEY_SECRET;

        try {
            // 2.获取client对象，对于阿里云Vod进行操作;
            DefaultAcsClient client = AliyuVodClientUtil.initVodClient(keyId, keySecret);

            // 3.新建删除音视频文件的request;
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoSourceId);

            DeleteVideoResponse response = client.getAcsResponse(request);
            System.out.println("RequestId : " + response.getRequestId());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliExcepiton(20002 , "删除音视频文件失败！！！");
        }

    }

    // 具体实现 ： 根据音视频id的list集合，来删除阿里云Vod中所有指定的音视频文件
    @Override
    public void deleteVideoVodByIds(List<String> videoSourceIds) {
        // 1.获取application.yml文件中，所配置的keyId和keySecret;
        String keyId = ConstantPropertiesGet.KEY_ID;
        String keySecret = ConstantPropertiesGet.KEY_SECRET;

        try {
            // 2.定义用来和阿里云Vod端进行交互的client对象
            DefaultAcsClient client = AliyuVodClientUtil.initVodClient(keyId , keySecret);

            // 3.定义 删除音视频的request和response
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();

            // 4.videoIds集合，转化为String类型的字符串
            // (每个videoId，都使用','进行分隔;)
            String videoSourceIdsStr = StringUtils.join(videoSourceIds.toArray() , ',');
            System.out.println("videoSourceIdsStr : " + videoSourceIdsStr);
            request.setVideoIds(videoSourceIdsStr);

            // 5.response获取该request的结果
            response = client.getAcsResponse(request);
            System.out.println("RequestId : " + response.getRequestId());

        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliExcepiton(20003 , "删除多个音视频文件失败！！！");
        }
    }
}
